package com.kaichaohulian.baocms.activity_newcreate;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.kaichaohulian.baocms.R;
import com.kaichaohulian.baocms.activity.ImagePagerActivity;
import com.kaichaohulian.baocms.utils.DBLog;

import java.util.ArrayList;
import java.util.List;

import butterknife.OnClick;
import me.iwf.photopicker.PhotoPickerActivity;
import me.iwf.photopicker.utils.PhotoPickerIntent;

/**
 * 主界面
 * Created by ljl on 2016/12/11.
 */
@SuppressLint("ValidFragment")
public class GongXiangXZActivity extends Activity implements View.OnClickListener {

    private ImageBaseAdapter ImageBaseAdapter;
    int index = 0;
    private int REQUEST_CODE = 200;
    public int CAN_SEE = 201;
    public int ADDRESS_CODE = 300;
    public int REMIND_CODE = 400;
    private List<String> list;
    private String TitleWhoSEE;
    private String privilegeType;
    private ArrayList<Integer> reminds;
    private ArrayList<Integer> users;
    GridView gridview;

    ImageView img_return;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gongxiangxz);

        gridview = (GridView) findViewById(R.id.gridview);
        img_return = (ImageView) findViewById(R.id.img_return);
        img_return.setOnClickListener(this);
        list = new ArrayList<>();
        ImageBaseAdapter = new ImageBaseAdapter();
        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (index != 9 && index - 1 == position) {
                    PhotoPickerIntent intent = new PhotoPickerIntent(GongXiangXZActivity.this);
                    intent.setPhotoCount(9);
                    intent.setShowCamera(true);
                    startActivityForResult(intent, REQUEST_CODE);
                } else {
                    ImagePagerActivity.ImageSize imageSize = new ImagePagerActivity.ImageSize(view.getMeasuredWidth(), view.getMeasuredHeight());
                    ImagePagerActivity.startImagePagerActivity(GongXiangXZActivity.this, list, position, imageSize);
                }
            }
        });

        gridview.setAdapter(ImageBaseAdapter);

    }

    @OnClick({R.id.img_return, R.id.edit_zhuti, R.id.edit_guanjianzi, R.id.edit_shopname, R.id.edit_price, R.id.edit_address, R.id.edit_shuoming, R.id.gridview, R.id.edit_shouhuoname, R.id.edit_phone, R.id.edit_address_shouho, R.id.edit_choulao, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_return:
                break;
            case R.id.edit_zhuti:
                break;
            case R.id.edit_guanjianzi:
                break;
            case R.id.edit_shopname:
                break;
            case R.id.edit_price:
                break;
            case R.id.edit_address:
                break;
            case R.id.edit_shuoming:
                break;
            case R.id.gridview:
                break;
            case R.id.edit_shouhuoname:
                break;
            case R.id.edit_phone:
                break;
            case R.id.edit_address_shouho:
                break;
            case R.id.edit_choulao:
                break;
            case R.id.btn_submit:
                break;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_return:
                finish();
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//                imm.showSoftInput(view,InputMethodManager.SHOW_FORCED);

                imm.hideSoftInputFromWindow(view.getWindowToken(), 0); //强制隐藏键盘
                break;
        }
    }

    public class ImageBaseAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            index = list.size() != 9 ? list.size() + 1 : list.size();
            return index;
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = LayoutInflater.from(GongXiangXZActivity.this).inflate(R.layout.release_imageitem, null);
            ImageView ImageView = (android.widget.ImageView) convertView.findViewById(R.id.image);
            if (index != 9 && index - 1 == position)
                ImageView.setBackgroundResource(R.mipmap.icon_releasetalk_add);
            else
                showImageView(ImageView, list.get(position));
            return convertView;
        }
    }

    private void showImageView(ImageView iamgeView, String avatar) {
        Glide.with(GongXiangXZActivity.this)
                .load(avatar)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(iamgeView);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
            if (data != null) {
                list = data.getStringArrayListExtra(PhotoPickerActivity.KEY_SELECTED_PHOTOS);
                ImageBaseAdapter.notifyDataSetChanged();
            }
        } else if (requestCode == CAN_SEE) {
            if (data != null) {
                TitleWhoSEE = data.getStringExtra("title");
                privilegeType = data.getStringExtra("privilegeType");
                users = data.getIntegerArrayListExtra("users");
//                status_tv.setText(TitleWhoSEE);
                DBLog.i("谁可以看", users);

            }
        } else if (requestCode == REMIND_CODE) {
            if (data != null) {
                reminds = data.getIntegerArrayListExtra("reminds");
                DBLog.i("提醒的好友发接受", this.reminds);
            }
        } else if (requestCode == ADDRESS_CODE) {
            if (data != null) {
                String addr = data.getStringExtra("addr");
                if (!TextUtils.isEmpty(addr)) {
//                    currentLocTV.setText(addr);
                }
            }
        }
    }

}
